# Step-by-step script to run an entire dating evening.
#
# In theory, this script is what you do once people have registered to
# run your event.
#
# In practice, this shows you the steps you need to take at various times.
# Read it to understand what the hell is going on with all this badly
# written R code.

source( "setup.R" )
#source( "Rs/setup.R" )


# Load in all past data history from pre-django.
source( "Rs/allPastData.R" )

# (If not done already) Load in the previous rounds of dating history
# from post-django era
source( "Rs/sqliteLoader.R" )
con = open_database()
save_date_table( table_name=paste( OLD_DATA_PATH,"run6_csv/date_table.csv",sep=""),
			 con, event = "macaw1" ) 
close_database( con )



# Set working directory to main PSD file collection
cat( "Run matches inside python world" )
# E.g., inside django main dir: python [eventname] makematrix

# HAND BUILD matches.csv file based on user comments of who
# knows whom.


# clean up
rm( list=ls() )

# Start from scratch, see what we get!  load.data reads the output of 
# the java file and updates banlist.csv (the list of who is here
# and whether they have paid and what their status is).
#source( "setup.R" )
source( "Rs/setup.R" )
source( "Rs/sqliteLoader.R" )

build.event.environment( INCLUDE_ALL = "NotNo" )


con = open_database()
make_nametag_file( con, EVENT_NAME, P$personID )
close_database( con )

ig = readline( "Now make your table.csv file.  Use the make.table.table function in tableMatcher.R to start.\nHit <return> to proceed.\n> " )


# NOW CHECK IN PEOPLE 
# Go and update the database using django (we hope)
# Check off everyone in the  "here" for everyone that is
# actually present and ready to date.  
# Hit return to continue when done.
ig = readline( "Check in folks with django admin interface.\nHit <return> to continue.\n> " )

# This reloads the data selecting only those who have In=="y".  
build.event.environment( INCLUDE_ALL="In" )

# get demographics for those present.
#source( "Rs/demographics.R" )
#F = compute.demographics( P )
#print( F )

match = readline( "Ready to make matches.  How many rounds of dating?  0 for none.\n> " )
if ( match > 0 ) {	
	source( "Rs/polyMatch.R" )
	trials = readline( "How many random trials should be generated?  More trials will find a better solution, in probability.  Try 10, if you have no idea.\n> " )
	make.matches( match, trials=trials )
	
	# now save the table to the database
	con = open_database()
	save_date_table( dts, con, event=EVENT_NAME, erase_old=TRUE )
	close_database(con)
}







if ( FALSE ) {
	load.data(db="testdb", INCLUDE_ALL="All")

	source( "Rs/polyMatch.R" )
	make.matches( 3, trials=1 )
}
